package cn.ds.assets.module.assets.service.dataQualityRuleSetValue;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.dataQualityRuleSetValue.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.dataQualityRuleSetValue.BaseDataQualityRuleSetValueDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.dataQualityRuleSetValue.BaseDataQualityRuleSetValueConvert;
import cn.ds.assets.module.assets.dal.mysql.dataQualityRuleSetValue.BaseDataQualityRuleSetValueMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 数据质量规则设置值 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseDataQualityRuleSetValueServiceImpl implements BaseDataQualityRuleSetValueService {

    @Resource
    private BaseDataQualityRuleSetValueMapper baseDataQualityRuleSetValueMapper;

    @Override
    public Long createBaseDataQualityRuleSetValue(BaseDataQualityRuleSetValueCreateReqVO createReqVO) {
        // 插入
        BaseDataQualityRuleSetValueDO baseDataQualityRuleSetValue = BaseDataQualityRuleSetValueConvert.INSTANCE.convert(createReqVO);
        baseDataQualityRuleSetValueMapper.insert(baseDataQualityRuleSetValue);
        // 返回
        return baseDataQualityRuleSetValue.getId();
    }

    @Override
    public void updateBaseDataQualityRuleSetValue(BaseDataQualityRuleSetValueUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseDataQualityRuleSetValueExists(updateReqVO.getId());
        // 更新
        BaseDataQualityRuleSetValueDO updateObj = BaseDataQualityRuleSetValueConvert.INSTANCE.convert(updateReqVO);
        baseDataQualityRuleSetValueMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseDataQualityRuleSetValue(Long id) {
        // 校验是否存在
        validateBaseDataQualityRuleSetValueExists(id);
        // 逻辑删除
        baseDataQualityRuleSetValueMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseDataQualityRuleSetValueExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseDataQualityRuleSetValueDO dataDo = baseDataQualityRuleSetValueMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseDataQualityRuleSetValueDO getBaseDataQualityRuleSetValue(Long id) {
        return baseDataQualityRuleSetValueMapper.selectById(id);
    }

    @Override
    public List<BaseDataQualityRuleSetValueDO> getBaseDataQualityRuleSetValueList(Collection<Long> ids) {
        return baseDataQualityRuleSetValueMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseDataQualityRuleSetValueDO> getBaseDataQualityRuleSetValuePage(BaseDataQualityRuleSetValuePageReqVO pageReqVO) {
        return baseDataQualityRuleSetValueMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseDataQualityRuleSetValueDO> getBaseDataQualityRuleSetValueList(BaseDataQualityRuleSetValueExportReqVO exportReqVO) {
        return baseDataQualityRuleSetValueMapper.selectList(exportReqVO);
    }

}
